import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const VueRouter = new  Router({
    // mode: 'history',
    routes: [
        {
            path: '/login',
            component: () => import("../view/login/login"),
            meta: { title: '登录'}
        },
        {
            path: '/',
            redirect: '/mainPool'
        },
        {
            path: '/',
            component: () => import('../view/home/home'),
            meta: {title: '系统',requireAuth: true},
            children: [
                {
                    path: '/mainPool',
                    component: () => import( '../view/home/mainPool'),
                    meta: { title: '首页',requireAuth: true}
                },
                {
                    path: '/lessonlist',
                    component: () => import( '../view/lesson/lessonlist'),
                    meta: { title: '课程',requireAuth: true}
                },{
                    path: '/lesson',
                    component: () => import( '../view/lesson/listinfo'),
                    meta: { title: '课程管理',requireAuth: true}
                },
                {
                    path: '/user',
                    component: () => import( '../view/user/listinfo'),
                    meta: { title: '用户管理',requireAuth: true}
                },
                {
                    path: '/college',
                    component: () => import( '../view/college/listinfo'),
                    meta: { title: '学院专业管理',requireAuth: true}
                },
            ]
        },
        {
            path: '/permission',
            component: () => import('../view/permission/permission'),
            meta: { title: '权限测试',requireAuth: true}
        },
        {
            path: '/403',
            component: () => import('../view/permission/403'),
            meta: { title: '403',requireAuth: true}
        },
        {
            path: '*',
            component:  () => import('../view/permission/404'),
            meta: { title: '404',requireAuth: true}
        }
    ]
})

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
//使用钩子函数对路由进行权限跳转
VueRouter.beforeEach((to, from, next) => {
    const info = JSON.parse(sessionStorage.getItem('userinfo')) || null
    const token = info ? info.token : null
    if(to.path == "/login"){
        token ? next("/") : next();
    }else{
        // requireAuth:可以在路由元信息指定哪些页面需要登录权限
        if(to.meta.requireAuth && token) {
            next();
        }else{
            next("/login");
        }
    }
});
export default VueRouter